<!--#
layout("/layouts/platform.html"){
#-->
<header class="header navbar bg-white shadow">
    <div class="btn-group tool-button">
        <a class="btn btn-primary navbar-btn" href="${base}/platform/shop/config/area/add" data-pjax><i class="ti-plus"></i>${msg['shop.config.area.add']}</a>
    </div>
    <div class="pull-right">
        <!--<div class="btn-group tool-button" style="padding-right: 5px;">-->
            <!--<a href="${base}/assets/common/vendor/city-picker/demo.html" target="_blank">Demo</a>-->
        <!--</div>-->
        <div class="btn-group tool-button">
            <button class="btn btn-info navbar-btn" onclick="build()"><i
                    class="ti-export"></i> ${msg['shop.config.area.build']}</button>
        </div>
        <div class="btn-group tool-button">
            <a class="btn btn-primary navbar-btn" href="${base}/platform/shop/config/area/sort" data-pjax><i
                    class="ti-share"></i> ${msg['shop.config.area.sort']}</a>
        </div>
    </div>
</header>
<div class="content-wrap">
    <div class="wrapper" style="min-height:500px;">
        <div class="row mb15 mt10">
            <div class="col-lg-12">
                <table id="unitTreeTable" class="table no-m">
                    <thead>
                    <tr>
                        <th class="col-md-2 pd-l-lg">
                            <span class="pd-l-sm ml20"></span>${msg['shop.config.area.name']}
                        </th>
                        <th class="col-md-2">${msg['shop.config.area.code']}</th>
                        <th class="col-md-2">${msg['shop.config.area.disable']}</th> 
                        <th class="col-md-2">${msg['shop.config.area.operation']}</th>
                    </tr>
                    </thead>
                    <tbody>
                    <!--# for(o in obj){
                   #-->
                    <tr data-tt-id="${o.id}" data-tt-parent-id="${o.parentId}" data-tt-branch="${o.hasChildren}">
                        <td>
                            <span class="pd-l-sm"></span>${o.name!}
                        </td>
                        <td>${o.code!}</td>
                        <td><!--# if (!o.disabled) {#-->
                              <i id="disabled_${o.id!}" class="fa fa-circle text-success ml5"></i>
                        <!--# } else {#-->
                              <i id="disabled_${o.id!}" class="fa fa-circle text-danger ml5"></i>
                       <!--#  }#-->
                         </td>
                        <td>
                            <div class="btn-group">
                                <button type="button" class="btn btn-default btn-xs dropdown-toggle"
                                        data-toggle="dropdown">
                                    <i class="ti-settings"></i>
                                    <span class="ti-angle-down"></span>
                                </button>
                                <ul class="dropdown-menu" role="menu">
                                     <li><a href="${base}/platform/shop/config/area/edit/${o.id}" data-pjax>${msg['shop.config.area.modification']}</a></li>
                                    <li><a href="javascript:;" onclick="del('${o.id}')">${msg['shop.config.area.del']}</a></li>
                                    <li class="divider"></li>
                                    <li><a href="${base}/platform/shop/config/area/add/${o.id}" data-pjax>${msg['shop.config.area.addchild']}</a></li>
                                    <li class="divider"></li>
                                    <li><a href="javascript:;" onclick="enable('${o.id}')">${msg['shop.config.area.enable']}</a></li>
                                    <li><a href="javascript:;" onclick="disable('${o.id}')">${msg['shop.config.area.disabled']}</a></li>
                                </ul>
                            </div>
                        </td>
                    </tr>
                   <!--#
                    }#-->
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<a class="exit-offscreen"></a>
<!-- 排序 -->
<div id="dialogSort" class="modal fade bs-modal-sm" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>
<!-- 删除 -->
<div id="dialogDelete" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">${msg['shop.config.area.delete']}</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        ${msg['shop.config.area.note']}
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"> ${msg['shop.config.area.cancel']}</button>
                <button id="ok" type="button" class="btn btn-primary" data-loading-text="${msg['shop.config.area.deleting']}">${msg['shop.config.area.determine']}</button>
            </div>
        </div>
    </div>
</div>
<!-- 详情 -->
<div id="dialogUnitDetail" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>
<script>
    var unitTreeTable;
    $(function () {
        $("#dialogUnitDetail").on("hidden.bs.modal", function () {
            $(this).removeData("bs.modal");
        });
        unitTreeTable = $("#unitTreeTable").treetable({
            expandable: true,
            onNodeExpand: function () {
                var node = this;
                var rows = "<tr data-tt-id=\"loading\" data-tt-parent-id=\"" + node.id + "\" data-tt-branch=\"false\"><td colspan='5'><img src=\"" + base + "/assets/img/loading.gif\"/>loading</td></tr>";
                unitTreeTable.treetable("loadBranch", node, rows);
                $.post("${base}/platform/shop/config/area/child/"+node.id,{}, function (data) {
                    unitTreeTable.treetable("unloadBranch", node);
                    unitTreeTable.treetable("loadBranch", node, data);
                });
            }
        });
    });
    
    function enable(id) {
        $.post("${base}/platform/shop/config/area/enable/" + id, {}, function (data) {
        	if (data.code == 0) {
        		$("#disabled_"+id).attr("class", "fa fa-circle text-success ml5");
        		Toast.success(data.msg);
            } else {
                Toast.error(data.msg);
            }
            btn.button("reset");
        }, "json");
    }

    function disable(id) {
        $.post("${base}/platform/shop/config/area/disable/" + id, {}, function (data) {
            if (data.code == 0) {
            	$("#disabled_"+id).attr("class", "fa fa-circle text-danger ml5");
            	Toast.success(data.msg);
            } else {
                Toast.error(data.msg);
            }
            btn.button("reset");
        }, "json");
    }
    
    function del(id) {
        var dialog = $("#dialogDelete");
        dialog.modal("show");
        dialog.find("#ok").unbind("click");
        dialog.find("#ok").bind("click", function (event) {
            var btn = $(this);
            btn.button("loading");
            $.post("${base}/platform/shop/config/area/delete/"+id, {}, function (data) {
                //重置按钮状态，关闭提示框
                dialog.modal("hide");
                if (data.code == 0) {
                    Toast.success(data.msg);
                    unitTreeTable.treetable("removeNode", id);
                    //从表格中移除此行
                } else {
                    Toast.error(data.msg);
                }
                btn.button("reset");
            }, "json");
        });
    }

    function build() {
        $.post("${base}/platform/shop/config/area/build", {}, function (data) {
            if (data.code == 0) {
                Toast.success(data.msg);
            } else {
                Toast.error(data.msg);
            }
        }, "json");
    }
</script>
<!--#}#-->